<template>
  <div>
    <div class="oder-number">订单：2105111108527762</div>
    <div class="pay-methods-container">
      <div class="pay-methods-title">支付方式</div>
      <div class="pay-methods-list">
        <div
          @click="activeMethod = 0"
          :class="['pay-method-block', { 'active-pay': activeMethod == 0 }]"
        >
          <div class="select-icon"></div>
        </div>
        <div
          @click="activeMethod = 1"
          :class="['pay-method-block', { 'active-pay': activeMethod == 1 }]"
        >
          <div class="select-icon"></div>
        </div>
      </div>
    </div>
    <div class="pay-button-container">
      <div class="pay-count">
        <span> 应付金额： </span>
        <span class="count-number">￥{{ $route.query.total }}</span>
      </div>
      <div class="pay-button" @click="toPay">立即支付</div>
      <div class="pay-question">付款有问题，点我</div>
    </div>
  </div>
</template>
<script>
import { buyCourse, getOrderById } from "@/services/pay";

export default {
  data() {
    return {
      activeMethod: 0,
      payResult: false,
      payOderNumber: "",
    };
  },
  methods: {
    toPay() {
      let userId = JSON.parse(localStorage.getItem("userInfo")).id;
      buyCourse({
        userId,
        courseIds: this.$route.query.courseIds,
        total: this.$route.query.total,
      }).then((res) => {
        this.payOderNumber = res.data.data;
        this.getPayResult();
        let routerJump = this.$router.resolve({
          path: "/payPage",
          query: { payForm: res.data.msg },
        });
        window.open(routerJump.href, "_blank");
      });
    },
    getPayResult() {
      let timeKey = setInterval(() => {
        getOrderById({ orderId: this.payOderNumber }).then((res) => {
          if (res.data.data == 1) {
            clearInterval(timeKey);
            this.toLearn();
          }
        });
      }, 2000);
    },
    toLearn() {
      this.$alert("支付成功", "提示", {
        confirmButtonText: "确定",
        callback: () => {
          this.$router.replace({ path: "/userhome/tollCourse" });
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.oder-number {
  height: 48px;
  border-bottom: 1px solid #b7bbbf;
}
.pay-methods-container {
  padding: 30px 12px 48px;
  border-bottom: 1px solid #d9dde1;
  .pay-methods-title {
    height: 46px;
    line-height: 30px;
    font-size: 16px;
    color: #07111b;
  }
  .pay-methods-list {
    display: flex;
    .pay-method-block {
      margin-right: 18px;
      position: relative;
      border-radius: 6px;
      width: 192px;
      height: 86px;
      background-color: #f3f5f7;
      border: 2px solid #f3f5f7;
      cursor: pointer;
      background-image: url("~@/assets/img/cart/pay_s.png");
      background-repeat: no-repeat;
      &:first-child {
        background-position: 33px -304px;
      }
      &:last-child {
        background-position: 30px -423px;
      }
    }
    .active-pay {
      border-color: #f01414;
      .select-icon {
        position: absolute;
        right: 0;
        bottom: 0;
        border-bottom: 20px solid red;
        border-left: 20px solid transparent;
        &::after {
          content: "√";
          color: white;
          position: absolute;
          right: 0px;
          bottom: -20px;
          font-size: 12px;
        }
      }
    }
  }
}

.pay-button-container {
  padding-top: 36px;
  height: 140px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  .pay-count {
    height: 24px;
    display: flex;
    align-items: center;
    .count-number {
      font-size: 24px;
      color: #f01414;
      font-weight: 700;
    }
  }
  .pay-button {
    width: 140px;
    height: 40px;
    font-size: 14px;
    font-weight: 700;
    color: #fff;
    background-color: #f01414;
    border-radius: 2px;
    text-align: center;
    line-height: 40px;
    cursor: pointer;
  }
  .pay-question {
    font-size: 12px;
    color: #4d555d;
  }
}
</style>